﻿using RotovapPlatform.Domain.Business.Recipes.Entities;
using RotovapPlatform.Domain.Common;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using static RotovapPlatform.Domain.Business.Machine.Enums.ExperimentConfigEnums;
using RotovapPlatform.Domain.Services.Scheduling;

namespace RotovapPlatform.Domain.Services.Data
{
	/// <summary>
	/// 数据记录服务接口
	/// 负责记录实验过程中的各类数据，包括设备参数、实验状态和结果
	/// </summary>
	public interface IDataRecordingDomainService
	{
		/// <summary>
		/// 订阅任务调度服务的事件
		/// </summary>
		/// <param name="taskSchedulingService">任务调度服务</param>
		void SubscribeToTaskSchedulingEvents(ITaskSchedulingDomainService taskSchedulingService);

		/// <summary>
		/// 记录设备参数
		/// </summary>
		/// <param name="deviceParameters">设备参数</param>
		/// <returns>操作结果</returns>
		Task<OperationResult> RecordDeviceParametersAsync(DeviceParameters deviceParameters);

		/// <summary>
		/// 记录实验状态
		/// </summary>
		/// <param name="experimentStatus">实验状态</param>
		/// <returns>操作结果</returns>
		Task<OperationResult> RecordExperimentStatusAsync(ExperimentStatusRecord experimentStatus);

		/// <summary>
		/// 记录实验结果
		/// </summary>
		/// <param name="experimentResult">实验结果</param>
		/// <returns>操作结果</returns>
		Task<OperationResult> RecordExperimentResultAsync(ExperimentResult experimentResult);

		/// <summary>
		/// 记录系统事件
		/// </summary>
		/// <param name="systemEvent">系统事件</param>
		/// <returns>操作结果</returns>
		Task<OperationResult> RecordSystemEventAsync(SystemEvent systemEvent);

		/// <summary>
		/// 记录报警信息
		/// </summary>
		/// <param name="alarm">报警信息</param>
		/// <returns>操作结果</returns>
		Task<OperationResult> RecordAlarmAsync(AlarmRecord alarm);

		/// <summary>
		/// 记录报警信息 - 简化方法
		/// </summary>
		/// <param name="alarmType">报警类型</param>
		/// <param name="message">报警消息</param>
		/// <param name="severity">报警严重程度</param>
		/// <param name="relatedExperimentId">相关联的实验ID，可为null</param>
		/// <returns>操作结果</returns>
		Task<OperationResult> RecordAlarmAsync(AlarmType alarmType, string message, AlarmSeverity severity = AlarmSeverity.Medium, Guid? relatedExperimentId = null);


		/// <summary>
		/// 获取设备参数历史记录
		/// </summary>
		/// <param name="experimentId">实验ID</param>
		/// <param name="startTime">开始时间</param>
		/// <param name="endTime">结束时间</param>
		/// <returns>设备参数历史记录</returns>
		Task<List<DeviceParameters>> GetDeviceParametersHistoryAsync(Guid? experimentId, DateTime startTime, DateTime endTime);

		/// <summary>
		/// 获取实验状态历史记录
		/// </summary>
		/// <param name="experimentId">实验ID</param>
		/// <param name="startTime">开始时间</param>
		/// <param name="endTime">结束时间</param>
		/// <returns>实验状态历史记录</returns>
		Task<List<ExperimentStatusRecord>> GetExperimentStatusHistoryAsync(Guid experimentId, DateTime startTime, DateTime endTime);

		/// <summary>
		/// 获取实验结果
		/// </summary>
		/// <param name="experimentId">实验ID</param>
		/// <returns>实验结果</returns>
		Task<ExperimentResult> GetExperimentResultAsync(Guid experimentId);

		/// <summary>
		/// 获取系统事件历史记录
		/// </summary>
		/// <param name="startTime">开始时间</param>
		/// <param name="endTime">结束时间</param>
		/// <param name="eventTypes">事件类型列表</param>
		/// <returns>系统事件历史记录</returns>
		Task<List<SystemEvent>> GetSystemEventHistoryAsync(DateTime startTime, DateTime endTime, List<SystemEventType> eventTypes = null);

		/// <summary>
		/// 获取报警历史记录
		/// </summary>
		/// <param name="startTime">开始时间</param>
		/// <param name="endTime">结束时间</param>
		/// <param name="alarmTypes">报警类型列表</param>
		/// <returns>报警历史记录</returns>
		Task<List<AlarmRecord>> GetAlarmHistoryAsync(DateTime startTime, DateTime endTime, List<AlarmType> alarmTypes = null);

		/// <summary>
		/// 导出实验数据
		/// </summary>
		/// <param name="experimentId">实验ID</param>
		/// <param name="exportFormat">导出格式</param>
		/// <param name="filePath">文件路径</param>
		/// <returns>操作结果</returns>
		Task<OperationResult> ExportExperimentDataAsync(Guid experimentId, ExportFormat exportFormat, string filePath);

		/// <summary>
		/// 导出实验组数据
		/// </summary>
		/// <param name="experimentGroupId">实验组ID</param>
		/// <param name="exportFormat">导出格式</param>
		/// <param name="filePath">文件路径</param>
		/// <returns>操作结果</returns>
		Task<OperationResult> ExportExperimentGroupDataAsync(Guid experimentGroupId, ExportFormat exportFormat, string filePath);

		/// <summary>
		/// 清理历史数据
		/// </summary>
		/// <param name="beforeDate">清理此日期之前的数据</param>
		/// <param name="dataTypes">数据类型列表</param>
		/// <returns>操作结果</returns>
		Task<OperationResult> CleanupHistoricalDataAsync(DateTime beforeDate, List<DataType> dataTypes);

		/// <summary>
		/// 备份数据
		/// </summary>
		/// <param name="backupPath">备份路径</param>
		/// <param name="dataTypes">数据类型列表</param>
		/// <returns>操作结果</returns>
		Task<OperationResult> BackupDataAsync(string backupPath, List<DataType> dataTypes);

		/// <summary>
		/// 恢复数据
		/// </summary>
		/// <param name="backupPath">备份路径</param>
		/// <returns>操作结果</returns>
		Task<OperationResult> RestoreDataAsync(string backupPath);

		/// <summary>
		/// 记录实验阶段变更
		/// </summary>
		/// <param name="experimentId">实验ID</param>
		/// <param name="stage">实验阶段</param>
		/// <param name="subStage">实验子阶段</param>
		/// <param name="changeType">变更类型</param>
		/// <returns>操作结果</returns>
		Task<OperationResult> RecordExperimentStageChangeAsync(
			Guid experimentId,
			ExperimentStage stage,
			ExperimentSubStage subStage,
			StageChangeType changeType);

		/// <summary>
		/// 记录实验事件
		/// </summary>
		/// <param name="experimentId">实验ID</param>
		/// <param name="eventType">事件类型</param>
		/// <param name="message">事件消息</param>
		/// <returns>操作结果</returns>
		Task<OperationResult> RecordExperimentEventAsync(
			Guid experimentId,
			string eventType,
			string message);

		/// <summary>
		/// 记录旋蒸数据
		/// </summary>
		/// <param name="experimentId">实验ID</param>
		/// <param name="vacuumLevel">真空度</param>
		/// <param name="oilBathTemperature">油浴温度</param>
		/// <param name="rotationSpeed">旋转速度</param>
		/// <param name="condensationTemperature">冷凝温度</param>
		/// <returns>操作结果</returns>
		Task<OperationResult> RecordRotovapDataAsync(
			Guid experimentId,
			double vacuumLevel,
			double oilBathTemperature,
			double rotationSpeed,
			double condensationTemperature);

		/// <summary>
		/// 记录设备状态变化
		/// </summary>
		/// <param name="experimentId">实验ID</param>
		/// <param name="deviceId">设备ID</param>
		/// <param name="status">设备状态</param>
		/// <returns>操作结果</returns>
		Task<OperationResult> RecordDeviceStatusChangeAsync(
			Guid experimentId,
			string deviceId,
			object status);

		/// <summary>
		/// 记录实验完成数据
		/// </summary>
		/// <param name="experimentId">实验ID</param>
		/// <param name="result">实验结果</param>
		/// <returns>操作结果</returns>
		Task<OperationResult> RecordExperimentCompletionAsync(
			Guid experimentId,
			ExperimentResult result);
	}

	/// <summary>
	/// 设备参数
	/// </summary>
	public class DeviceParameters
	{
		/// <summary>
		/// 记录ID
		/// </summary>
		public Guid Id { get; set; } = Guid.NewGuid();

		/// <summary>
		/// 实验ID
		/// </summary>
		public Guid? ExperimentId { get; set; }

		/// <summary>
		/// 记录时间
		/// </summary>
		public DateTime Timestamp { get; set; } = DateTime.Now;

		/// <summary>
		/// 油浴温度
		/// </summary>
		public double OilBathTemperature { get; set; }

		/// <summary>
		/// 冷凝温度
		/// </summary>
		public double CondensationTemperature { get; set; }

		/// <summary>
		/// 旋转速度
		/// </summary>
		public double RotationSpeed { get; set; }

		/// <summary>
		/// 真空度
		/// </summary>
		public double Vacuum { get; set; }

		/// <summary>
		/// 设备模式
		/// </summary>
		public string DeviceMode { get; set; }

		/// <summary>
		/// 设备状态
		/// </summary>
		public string DeviceStatus { get; set; }

		/// <summary>
		/// 机械手状态
		/// </summary>
		public string RobotArmStatus { get; set; }

		/// <summary>
		/// 机械手位置
		/// </summary>
		public string RobotArmPosition { get; set; }

		/// <summary>
		/// 其他参数
		/// </summary>
		public Dictionary<string, object> AdditionalParameters { get; set; } = new Dictionary<string, object>();
	}

	/// <summary>
	/// 实验状态记录
	/// </summary>
	public class ExperimentStatusRecord
	{
		/// <summary>
		/// 记录ID
		/// </summary>
		public Guid Id { get; set; } = Guid.NewGuid();

		/// <summary>
		/// 实验ID
		/// </summary>
		public Guid ExperimentId { get; set; }

		/// <summary>
		/// 记录时间
		/// </summary>
		public DateTime Timestamp { get; set; } = DateTime.Now;

		/// <summary>
		/// 当前步骤
		/// </summary>
		public string CurrentStep { get; set; }

		/// <summary>
		/// 状态
		/// </summary>
		public string Status { get; set; }

		/// <summary>
		/// 进度百分比
		/// </summary>
		public double Progress { get; set; }

		/// <summary>
		/// 剩余时间（分钟）
		/// </summary>
		public int RemainingTimeMinutes { get; set; }

		/// <summary>
		/// 错误信息
		/// </summary>
		public string ErrorMessage { get; set; }

		/// <summary>
		/// 其他状态信息
		/// </summary>
		public Dictionary<string, object> AdditionalStatusInfo { get; set; } = new Dictionary<string, object>();
	}

	/// <summary>
	/// 旋蒸数据记录
	/// </summary>
	public class RotovapData
	{
		/// <summary>
		/// 记录ID
		/// </summary>
		public Guid Id { get; set; }

		/// <summary>
		/// 实验ID
		/// </summary>
		public Guid ExperimentId { get; set; }

		/// <summary>
		/// 真空度
		/// </summary>
		public double VacuumLevel { get; set; }

		/// <summary>
		/// 油浴温度
		/// </summary>
		public double OilBathTemperature { get; set; }

		/// <summary>
		/// 旋转速度
		/// </summary>
		public double RotationSpeed { get; set; }

		/// <summary>
		/// 冷凝温度
		/// </summary>
		public double CondensationTemperature { get; set; }

		/// <summary>
		/// 时间戳
		/// </summary>
		public DateTime Timestamp { get; set; }
	}

	/// <summary>
	/// 实验结果
	/// </summary>
	public class ExperimentResult
	{
		/// <summary>
		/// 实验ID
		/// </summary>
		public Guid ExperimentId { get; set; }

		/// <summary>
		/// 是否成功
		/// </summary>
		public bool IsSuccessful { get; set; }

		/// <summary>
		/// 产品收集量(ml)
		/// </summary>
		public double ProductCollectionAmount { get; set; }

		/// <summary>
		/// 收率(%)
		/// </summary>
		public double Yield { get; set; }

		/// <summary>
		/// 总执行时间(分钟)
		/// </summary>
		public double TotalExecutionTimeMinutes { get; set; }

		/// <summary>
		/// 错误信息
		/// </summary>
		public string ErrorMessage { get; set; }

		/// <summary>
		/// 备注
		/// </summary>
		public string Notes { get; set; }

		/// <summary>
		/// 完成时间
		/// </summary>
		public DateTime CompletionTime { get; set; }
	}

	/// <summary>
	/// 系统事件
	/// </summary>
	public class SystemEvent
	{
		/// <summary>
		/// 事件ID
		/// </summary>
		public Guid Id { get; set; } = Guid.NewGuid();

		/// <summary>
		/// 记录时间
		/// </summary>
		public DateTime Timestamp { get; set; } = DateTime.Now;

		/// <summary>
		/// 事件类型
		/// </summary>
		public SystemEventType EventType { get; set; }

		/// <summary>
		/// 事件源
		/// </summary>
		public string Source { get; set; }

		/// <summary>
		/// 事件消息
		/// </summary>
		public string Message { get; set; }

		/// <summary>
		/// 事件详情
		/// </summary>
		public string Details { get; set; }

		/// <summary>
		/// 相关实验ID
		/// </summary>
		public Guid? RelatedExperimentId { get; set; }

		/// <summary>
		/// 相关用户
		/// </summary>
		public string RelatedUser { get; set; }

		/// <summary>
		/// 严重程度
		/// </summary>
		public EventSeverity Severity { get; set; }

		/// <summary>
		/// 其他事件数据
		/// </summary>
		public Dictionary<string, object> AdditionalEventData { get; set; } = new Dictionary<string, object>();
	}

	/// <summary>
	/// 报警记录
	/// </summary>
	public class AlarmRecord
	{
		/// <summary>
		/// 报警ID
		/// </summary>
		public Guid Id { get; set; } = Guid.NewGuid();

		/// <summary>
		/// 记录时间
		/// </summary>
		public DateTime Timestamp { get; set; } = DateTime.Now;

		/// <summary>
		/// 报警类型
		/// </summary>
		public AlarmType AlarmType { get; set; }

		/// <summary>
		/// 报警源
		/// </summary>
		public string Source { get; set; }

		/// <summary>
		/// 报警消息
		/// </summary>
		public string Message { get; set; }

		/// <summary>
		/// 报警详情
		/// </summary>
		public string Details { get; set; }

		/// <summary>
		/// 相关实验ID
		/// </summary>
		public Guid? RelatedExperimentId { get; set; }

		/// <summary>
		/// 严重程度
		/// </summary>
		public AlarmSeverity Severity { get; set; }

		/// <summary>
		/// 是否已确认
		/// </summary>
		public bool IsAcknowledged { get; set; }

		/// <summary>
		/// 确认时间
		/// </summary>
		public DateTime? AcknowledgedTime { get; set; }

		/// <summary>
		/// 确认用户
		/// </summary>
		public string AcknowledgedBy { get; set; }

		/// <summary>
		/// 确认备注
		/// </summary>
		public string AcknowledgementNotes { get; set; }

		/// <summary>
		/// 其他报警数据
		/// </summary>
		public Dictionary<string, object> AdditionalAlarmData { get; set; } = new Dictionary<string, object>();
	}

	/// <summary>
	/// 系统事件类型
	/// </summary>
	public enum SystemEventType
	{
		/// <summary>
		/// 系统启动
		/// </summary>
		SystemStartup,

		/// <summary>
		/// 系统关闭
		/// </summary>
		SystemShutdown,

		/// <summary>
		/// 用户登录
		/// </summary>
		UserLogin,

		/// <summary>
		/// 用户登出
		/// </summary>
		UserLogout,

		/// <summary>
		/// 实验开始
		/// </summary>
		ExperimentStarted,

		/// <summary>
		/// 实验暂停
		/// </summary>
		ExperimentPaused,

		/// <summary>
		/// 实验恢复
		/// </summary>
		ExperimentResumed,

		/// <summary>
		/// 实验完成
		/// </summary>
		ExperimentCompleted,

		/// <summary>
		/// 实验取消
		/// </summary>
		ExperimentCancelled,

		/// <summary>
		/// 实验失败
		/// </summary>
		ExperimentFailed,

		/// <summary>
		/// 设备模式变更
		/// </summary>
		DeviceModeChanged,

		/// <summary>
		/// 设备状态变更
		/// </summary>
		DeviceStatusChanged,

		/// <summary>
		/// 配置变更
		/// </summary>
		ConfigurationChanged,

		/// <summary>
		/// 数据备份
		/// </summary>
		DataBackup,

		/// <summary>
		/// 数据恢复
		/// </summary>
		DataRestore,

		/// <summary>
		/// 系统维护
		/// </summary>
		SystemMaintenance,

		/// <summary>
		/// 软件更新
		/// </summary>
		SoftwareUpdate,

		/// <summary>
		/// 其他
		/// </summary>
		Other
	}

	/// <summary>
	/// 报警类型
	/// </summary>
	public enum AlarmType
	{
		/// <summary>
		/// 操作失败
		/// </summary>
		ActionFailure,

		/// <summary>
		/// 温度超限
		/// </summary>
		TemperatureOutOfRange,

		/// <summary>
		/// 压力超限
		/// </summary>
		PressureOutOfRange,

		/// <summary>
		/// 旋转速度超限
		/// </summary>
		RotationSpeedOutOfRange,

		/// <summary>
		/// 真空度超限
		/// </summary>
		VacuumOutOfRange,

		/// <summary>
		/// 设备故障
		/// </summary>
		DeviceMalfunction,

		/// <summary>
		/// 传感器故障
		/// </summary>
		SensorMalfunction,

		/// <summary>
		/// 通信故障
		/// </summary>
		CommunicationError,

		/// <summary>
		/// 物料错误
		/// </summary>
		MaterialError,

		/// <summary>
		/// 紧急停止
		/// </summary>
		EmergencyStop,

		/// <summary>
		/// 系统错误
		/// </summary>
		SystemError,

		/// <summary>
		/// 其他
		/// </summary>
		Other
	}

	/// <summary>
	/// 事件严重程度
	/// </summary>
	public enum EventSeverity
	{
		/// <summary>
		/// 信息
		/// </summary>
		Information,

		/// <summary>
		/// 警告
		/// </summary>
		Warning,

		/// <summary>
		/// 错误
		/// </summary>
		Error,

		/// <summary>
		/// 严重
		/// </summary>
		Critical
	}

	/// <summary>
	/// 报警严重程度
	/// </summary>
	public enum AlarmSeverity
	{
		/// <summary>
		/// 低
		/// </summary>
		Low,

		/// <summary>
		/// 中
		/// </summary>
		Medium,

		/// <summary>
		/// 高
		/// </summary>
		High,

		/// <summary>
		/// 紧急
		/// </summary>
		Critical
	}

	/// <summary>
	/// 导出格式
	/// </summary>
	public enum ExportFormat
	{
		/// <summary>
		/// CSV
		/// </summary>
		CSV,

		/// <summary>
		/// Excel
		/// </summary>
		Excel,

		/// <summary>
		/// JSON
		/// </summary>
		JSON,

		/// <summary>
		/// XML
		/// </summary>
		XML,

		/// <summary>
		/// PDF
		/// </summary>
		PDF
	}

	/// <summary>
	/// 数据类型
	/// </summary>
	public enum DataType
	{
		/// <summary>
		/// 设备参数
		/// </summary>
		DeviceParameters,

		/// <summary>
		/// 实验状态
		/// </summary>
		ExperimentStatus,

		/// <summary>
		/// 实验结果
		/// </summary>
		ExperimentResults,

		/// <summary>
		/// 系统事件
		/// </summary>
		SystemEvents,

		/// <summary>
		/// 报警
		/// </summary>
		Alarms,

		/// <summary>
		/// 所有数据
		/// </summary>
		All
	}

	/// <summary>
	/// 实验阶段变更记录
	/// </summary>
	public class ExperimentStageChange
	{
		/// <summary>
		/// 记录ID
		/// </summary>
		public Guid Id { get; set; }

		/// <summary>
		/// 实验ID
		/// </summary>
		public Guid ExperimentId { get; set; }

		/// <summary>
		/// 实验阶段
		/// </summary>
		public ExperimentStage Stage { get; set; }

		/// <summary>
		/// 实验子阶段
		/// </summary>
		public ExperimentSubStage SubStage { get; set; }

		/// <summary>
		/// 变更类型
		/// </summary>
		public StageChangeType ChangeType { get; set; }

		/// <summary>
		/// 时间戳
		/// </summary>
		public DateTime Timestamp { get; set; }
	}
}